package com.situ.myplayer.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.IOException;

/**
 * Created by Gao on 2024/6/26.
 */

public class MusicService extends Service{
    private static final String TAG = "MusicService";
    private MediaPlayer mMediaPlayer;

    @Override
    public void onCreate() {
        super.onCreate();
        mMediaPlayer = new MediaPlayer();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicControll();
    }

    //这个类的对象会通过onBind方法返回给Activity
    public class MusicControll extends Binder {
        public void init(String path) {
            Log.d(TAG, "init: path: " + path);
            try {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(path);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void play() {
            Log.d(TAG, "play:");
            mMediaPlayer.start();
        }

        public void pause() {
            Log.d(TAG, "pause:");
            mMediaPlayer.pause();
        }

        public boolean isPlaying() {
            boolean isPlaying = mMediaPlayer.isPlaying();
            Log.d(TAG, "isPlaying: " + isPlaying);
            return isPlaying;
        }

    }



}
